<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card shadow="never">
        <el-row type="flex" justify="space-between" style="width: 100%">
          <el-col :span="21">
            <el-form
              :inline="true"
              :model="formInline"
              class="demo-form-inline"
            >
              <el-form-item label="关键字">
                <el-input
                  v-model="formInline.keyword"
                  placeholder="根据文章标题搜索"
                ></el-input>
              </el-form-item>
              <el-form-item label="状态">
                <el-select v-model="formInline.state" placeholder="请选择">
                  <el-option label="启用" :value="1"></el-option>
                  <el-option label="禁用" :value="0"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item>
                <el-button @click="noSubmit">清除</el-button>
                <el-button type="primary" @click="onSubmit">搜索</el-button>
              </el-form-item>
            </el-form>
          </el-col>
          <el-col :span="3">
            <el-button type="success small" @click="showDialog"
              ><i class="el-icon-edit"></i>新增技巧</el-button
            >
          </el-col>
        </el-row>
        <el-row class="grid-content bg-purple-light" type="flex" align="middle"
          ><i class="el-icon-info"></i>数据一共{{ totals }}条</el-row
        >
      </el-card>
      <el-table :data="artcilesList" style="width: 100%">
        <el-table-column type="index" label="序号" width="80">
        </el-table-column>
        <el-table-column prop="title" label="文章标题" width="250">
          <!-- <template #default="{row}">
          <div>
         {{ row.title }}
         <a href="#" class="el-icon-film" ></a>
          </div>
        </template> -->
          <template #default="{row}">
            {{ row.title }}
            <i
              @click="openVideo(row)"
              v-if="row.videoURL"
              class="fa fa-film"
              aria-hidden="true"
            ></i>
          </template>
        </el-table-column>
        <el-table-column prop="visits" label="阅读数" width="150">
        </el-table-column>
        <el-table-column prop="username" label="录入人" width="230">
        </el-table-column>
        <el-table-column prop="createTime" label="录入时间" width="250">
          <template #default="{row}">
            {{ formatDate(row.createTime) }}
          </template>
        </el-table-column>
        <el-table-column prop="state" label="状态">
          <template #default="{row}">{{
            tagEnum[row.state]
          }}</template></el-table-column
        >
        <el-table-column prop="operation" label="操作">
          <template #default="{row}">
            <el-button type="text" @click="previewClick(row)">预览</el-button>
            <el-button type="text" @click="changeState(row)">{{
              row.state ? "禁用" : "启用"
            }}</el-button>
            <el-button
              type="text"
              :disabled="row.state === 1"
              @click="reviseSubmit(row)"
              >修改</el-button
            >
            <el-button
              type="text"
              :disabled="row.state === 1"
              @click="deleteSubmit(row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-row type="flex" justify="end">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="this.params.page"
          background
          :page-sizes="[5, 10, 20, 50]"
          :page-size="this.params.pagesize"
          layout=" prev, pager, next,sizes"
          :total="totals"
        >
        </el-pagination>
      </el-row>
    </div>
    <el-dialog
      title="提示"
      :visible="dialogVisible"
      width="50%"
      @close="handleClose"
      @open="openDialog"
    >
      <el-form :model="form" :rules="rules" ref="form" label-width="80px">
        <el-form-item label="文章标题" prop="title">
          <el-input
            v-model="form.title"
            placeholder="请输入文章标签"
          ></el-input>
        </el-form-item>
        <el-form-item label="文章内容" prop="articleBody">
          <quill-editor v-model="form.articleBody"> </quill-editor>
        </el-form-item>
        <el-form-item label="视频地址" prop="videoURL">
          <el-input
            v-model="form.videoURL"
            placeholder="请输入视频地址"
            width="80"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
    <el-button type="text" @click="dialogVisible = true"></el-button>
    <el-dialog
      title="预览文章"
      :visible="showDialog1"
      width="50%"
      @close="CancelBtn"
    >
      <el-row>
        <h2>{{ previewList.title }}</h2>
      </el-row>
      <el-row>
        <span style="margin-left: 10px">{{
          previewList.createTime | parseTime
        }}</span>
        <span style="margin-left: 10px">超级管理员</span>
        <span style="margin-left: 10px"
          ><i class="el-icon-view" style="margin-right: 10px"></i
          >{{ previewList.visits }}</span
        >
      </el-row>
      <el-row style="margin-top: 10px">
        <el-card
          shadow="never"
          style="background: #f5f5f5; border-top: 1px #ccc dashed"
        >
          <div v-html="previewList.articleBody"></div>
        </el-card>
      </el-row> </el-dialog
    >>
    <div class="video" v-if="play">
      <div class="video-box">
        <video autoplay :src="form.videoURL"></video>
      </div>
      <div class="video-ico" @click="play = false">
        <span class="el-icon-close"></span>
      </div>
    </div>
  </div>
</template>

<script>
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import { quillEditor } from 'vue-quill-editor'
import {
  list,
  changeState,
  remove,
  add,
  update,
  detail
} from '@/api//hmmm/articles.js'
import dayjs from 'dayjs'
export default {
  components: {
    quillEditor
  },
  data () {
    return {
      formInline: {
        keyword: '',
        state: ''
      },
      showDialog1: false,
      previewList: {},
      input: '',
      artcilesList: [],
      artcilesdata: '',
      params: {
        page: 1,
        pagesize: 10
      },
      totals: 0,
      tagEnum: {
        1: '已启用',
        0: '已禁用'
      },
      dialogVisible: false,
      form: {
        title: '',
        articleBody: '',
        videoURL: '',
        id: ''
      },
      play: false,
      // 表单校验的规则
      rules: {
        title: [{ required: true, message: '请输入文章标题', trigger: 'blur' }],
        articleBody: [
          { required: true, message: '请输入文章内容', trigger: 'blur' }
        ]
      },
      dialogVisible1: false
    }
  },
  created () {
    this.getArtclesList()
  },
  methods: {
    async getArtclesList () {
      const res = await list(this.params)
      console.log(res)
      this.artcilesList = res.data.items
      this.totals = res.data.counts
    },
    formatDate (value) {
      return dayjs(value).format('YYYY-MM-DD HH:mm:ss')
    },
    noSubmit () {
      this.formInline = {
        keyword: '',
        state: ''
      }
      this.getArtclesList()
    },
    async onSubmit () {
      const res = await list({
        ...this.params,
        ...this.formInline
      })
      this.artcilesList = res.data.items
      this.totals = res.data.counts
      console.log('哈哈哈哈', res)
    },
    async changeState (row) {
      if (row.state === 1) {
        row.state = 0
        this.disabled = false
      } else {
        row.state = 1
        this.disabled = true
      }
      // 发起请求，更新后台数据
      await changeState(row)
      this.$message.success('操作成功')
    },
    submit () {
      // 兜底校验
      this.$refs.form.validate(async valid => {
        if (!valid) return
        // 校验通过
        // 发起请求
        if (this.form.id) {
          await update(this.form)
        } else {
          await add({
            title: this.form.title,
            articleBody: this.form.articleBody,
            videoURL: this.form.videoURL
          })
        }
        // 提示用户
        this.$message.success('操作成功')
        this.handleClose()
        this.getArtclesList()
      })
    },
    handleClose () {
      this.dialogVisible = false
      this.form = {
        title: '',
        articleBody: '',
        videoURL: '',
        id: ''
      }
    },
    // 预览按钮
    async previewClick (id) {
      this.showDialog1 = true
      const res = await detail(id)
      console.log(res)
      this.previewList = res.data
    },
    CancelBtn () {
      this.showDialog1 = false
    },
    Close () {
      this.dialogVisible1 = false
    },
    // 修改按钮
    reviseSubmit (row) {
      this.form.title = row.title
      this.form.articleBody = row.articleBody
      this.form.id = row.id
      this.form.videoURL = row.videoURL
      this.dialogVisible = true
    },
    // 删除按钮
    deleteSubmit (id) {
      // 询问用户
      this.$confirm('此操作将永久删除该文章, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        // 发起请求
        await remove({ id: id })
        // 提示用户
        this.$message.success('删除员工成功')
        // 刷新表格数据
        this.getArtclesList()
      })
    },
    openDialog () {},
    showDialog () {
      this.dialogVisible = true
    },
    handleSizeChange (val) {
      // console.log(`每页 ${val} 条`)
      this.params.pagesize = val
      this.getArtclesList()
    },
    handleCurrentChange (val) {
      // console.log(`当前页: ${val}`)
      this.params.page = val
      this.getArtclesList()
    },
    openVideo (row) {
      this.play = true
      this.form.videoURL = row.videoURL
    }
  }
}
</script>

<style scoped lang="scss">
.grid-content {
  border-radius: 4px;
  min-height: 36px;
  font-size: 13px;
  color: #909399;
}
.el-icon-info {
  padding: 0 8px;
}
.bg-purple-light {
  background: #f0f2f5;
}
.quill-editor {
  ::v-deep .ql-editor {
    height: 300px;
  }
}
.color {
  background: #f0f2f5;
  padding: 20px;
}
.fa-film:before {
  color: #0245a8;
}
.video {
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.3);
  overflow: hidden;
  z-index: 9999;
  .video-ico {
    width: 50px;
    height: 50px;
    position: absolute;
    top: 30px;
    left: 50%;
    transform: translate(-50%, 0);
    background: rgba(0, 0, 0, 0.4);
    box-shadow: 0 0 5px rgba(0, 0, 0, 0.4);
    border-radius: 50%;
    text-align: center;
    line-height: 50px;
    color: #fff;
    font-size: 20px;
  }
  .video-box {
    width: 800px;
    height: 600px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    video {
      width: 100%;
      height: 100%;
    }
  }
}
</style>
